package com.itic.appbase.applications.sys.msg.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.applications.sys.msg.persistence.dao.MsgDao;
import com.itic.appbase.applications.sys.msg.persistence.model.Msg;
import com.itic.appbase.applications.sys.msg.persistence.model.MsgExample;
import com.itic.appbase.applications.sys.msgreaded.persistence.dao.MsgReadedDao;
import com.itic.appbase.applications.sys.msgreaded.persistence.model.MsgReaded;
import com.itic.appbase.applications.sys.msgreaded.persistence.model.MsgReadedExample;
import com.itic.appbase.applications.sys.msgreaded.persistence.model.MsgReadedExample.Criteria;
import com.itic.appbase.applications.sys.user.persistence.dao.SysUserDao;
import com.itic.appbase.applications.sys.user.persistence.model.SysUser;


@Service
@Transactional
public class MsgService
     extends CrudService<MsgDao,Msg,MsgExample> {
    @Autowired
    private MsgDao msgDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private MsgReadedDao msgReadedDao;
    
    /**
     * 用户消息提醒减少一条
     * @param user
     * @param msg
     * @return
     */
	public String readMsg(SysUser user, Msg msg) {
		MsgReadedExample example = new MsgReadedExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andSysUserIdEqualTo(user.getId());
		criteria.andMsgSysIdEqualTo(msg.getId());
		List<MsgReaded> list = msgReadedDao.selectByExample(example);
		if( list.isEmpty() ){
			Integer msgTips = user.getMsgTips()==null?1:user.getMsgTips()-1;
			user.setMsgTips(msgTips);
			sysUserDao.updateByPrimaryKeySelective(user);
		}
		return SUCCESS;
	}


}